<?php

class lsoDesign
{
	var $translator;
	var $sysinfo;


	function lsoDesign($i18n,$info){
		$this->translator=$i18n;
		$this->sysinfo=$info;
	}


	function printHead($lang,$i18n)
	{
		$host=$this->sysinfo->getServerName();
		$ip=$this->sysinfo->getServerAddress();
		print "<div id='header'>".$this->translator->getString("Linux Server Observatory for")." $host ($ip)</div>";


		print "<div id='navi'>";
		print "<a href='index.php?show=sys&lang=$lang'>";
		print $this->translator->getString("System Informations")." </a> | <a href='index.php?show=user&lang=$lang'>";
		print $this->translator->getString("Usermanagment")."</a> | <a href='index.php?show=proc&lang=$lang'>";
		print $this->translator->getString("Processes")."</a> | <a href='index.php?show=dev&lang=$lang'>";
		print $this->translator->getString("Devices")."</a> | <a href='index.php?show=cron&lang=$lang'>";
		print $this->translator->getString("Cronjobs")."</a> | <a href='index.php?show=php&lang=$lang'>phpinfo()</a> | ";

		print "<a href='index.php?show=ini	t&lang=$lang'>".$this->translator->getString("Runlevel")."</a> | ";

		print "<a href='index.php?show=fs&lang=$lang'>".$this->translator->getString("Filesystems")."</a>";


		print "</div><div id='lang'>";
		print "<form action='index.php' method='get'><select name='lang'>";


		$languages=$i18n->getLanguages();

		print "<option>$lang</option>";

		foreach($languages as $key=>$val)
		{
			if($key!=$lang)
				print "<option>$key</option>";
		}

		print "</select>&nbsp;<input type='submit' value='Change'></form></div>";

		//just a spacer
		print "<div id='box'></div>";
	}


	function printCronInfo()
	{

		print "<div id='table_header'>";
		print "<img src='images/16x16/cron.png' alt='cron' /> Crontab";
		print "</div><div id='table_box'>";

		//read /etc/crontab
		print "\n<table class='data_table'>";

		$cron_lines=$this->sysinfo->getGlobalCrontab();
		foreach($cron_lines as $line){
			print "<tr><td>$line</td></tr>\n";
		}


		print "</table>";

		print "</div><br /><br />";


		print "<div id='table_header'>cron.hourly:</div><div id='table_box'><table class='data_table'>";
		$cron_lines=$this->sysinfo->getCronHourly();
		foreach($cron_lines as $line){
			print "<tr><td>$line</td></tr>\n";
		}
		print "</table></div><br /><br />";


		print "<div id='table_header'>cron.daily:</div><div id='table_box'><table class='data_table'>";
		$cron_lines=$this->sysinfo->getCronDaily();
		foreach($cron_lines as $line){
			print "<tr><td>$line</td></tr>\n";
		}
		print "</table></div><br /><br />";


		print "<div id='table_header'>cron.weekly:</div><div id='table_box'><table class='data_table'>";
		$cron_lines=$this->sysinfo->getCronWeekly();
		foreach($cron_lines as $line){
			print "<tr><td>$line</td></tr>\n";
		}
		print "</table></div><br /><br />";


		print "<div id='table_header'>cron.monthly:</div><div id='table_box'><table class='data_table'>";
		$cron_lines=$this->sysinfo->getCronMonthly();
		foreach($cron_lines as $line){
			print "<tr><td>$line</td></tr>\n";
		}
		print "</table></div><br /><br />";
	}


	function printMountedFilesystems()
	{

		print "<div id='table_header'>";
		print "<img src='images/16x16/fs.png' alt='fs' /> ".$this->translator->getString("Filesystems");
		print "</div><div id='table_box'>";
		print "<table class='data_table' cellpadding='5'>";

		$mountpoint=$this->translator->getString("Mountpoint");
		$fs_type=$this->translator->getString("FS-Type");
		$options=$this->translator->getString("Options");
		$free=$this->translator->getString("Free space");
		$dev=$this->translator->getString("Device");

		print "<tr><th>$dev</th><th>$mountpoint</th><th>$fs_type</th><th>$options</th><th>$free</th>";

		$fs=$this->sysinfo->getMountedFilesystems();

		foreach($fs as $key => $value)
		{
			print "<tr><td>$key</td><td>".$value['mountpoint']."</td><td>".$value['type']."</td><td>".$value['options']."</td><td>".$value['freespace']." mb </td></tr>";
		}
		print "</table>";
	}

	function printInitInfo()
	{

		print "<div id='table_header'>";
		print "<img src='images/16x16/fs.png' alt='fs' />".$this->translator->getString("Filesystems");
		print "</div><div id='table_box'>";
		print "<table class='data_table' cellpadding='3'>";


		print "<tr><th>$dev</th><th>$mountpoint</th><th>$fs_type</th><th>$options</th><th>$free</th>";

		$init=$this->sysinfo->getRunlevel();

		print "<tr><th>Service</th>";

		$run=array("0","1","2","3","4","5","6","S");

		foreach($run as $key)
		{
			print "<th>$key</th>";
		}
		print "</tr>";

		foreach($init as $key => $value)
		{
			print "<tr>";
			print "<td>$key</td>";
			foreach($run as $k){
				if($init[$key][$k]['start']=="1" && $init[$key][$k]['kill']=="1")
					print "<td>B</td>";
				elseif($init[$key][$k]['kill']=="1")
					print "<td>K</td>";
				elseif($init[$key][$k]['start']=="1")
					print "<td>S</td>";
				else
					print "<td></td>";
			}
			print "</tr>";
		}


		print "</table>";
	}



	function printDeviceInfos()
	{

		print "<div id='table_header'>";
		print "<img src='images/16x16/dev.png' alt='dev' />".$this->translator->getString("Devices:");
		print "</div><div id='table_box'>";
		print "<table class='data_table'><tr><td>".$this->translator->getString("PCI-Devices:")."</td><td></td></tr>\n";

		$pci=$this->sysinfo->getPciDevices();

		foreach($pci as $line)
		{
			print "<tr><td></td><td>$line</td></tr>\n";
		}




		print "<tr><td>".$this->translator->getString("USB-Devices:")."</td><td></td></tr>";

		$usb=$this->sysinfo->getUsbDevices();


		foreach($usb as $line)
		{
			print "<tr><td></td><td>$line</td></tr>\n";
		}

		print "</table></div>\n";
	}

	function printUserInfos()
	{

		print "<div id='table_header'>";
		print "<img src='images/16x16/user.png' alt='user'>".$this->translator->getString("User");
		print "</div>";

		print "<div id='table_box'>";

		print "\n<table class='data_table' width='100%'>";

		$users=$this->sysinfo->getSystemUsers();

		foreach($users as $user)
		{
			print "<tr>\n";

			print "<td>".$user["login"]."</td>\n";
			print "<td>".$user["uid"]."</td>\n";
			print "<td>".$user["gid"]."</td>\n";
			print "<td>".$user["gcos"]."</td>\n";
			print "<td>".$user["home"]."</td>\n";
			print "<td>".$user["shell"]."</td>";
			print "</tr>\n";
		}
		print "</table></div>";
	}

	function printProcessInfos()
	{
		print "<div id='table_header'>";
		print "<img src='images/16x16/proc.png' alt='proc' />".$this->translator->getString("Processes");

		$procs=$this->sysinfo->getProcesses();
		print "</div><div id='table_box'><table class='data_table'>";

		foreach($procs as $proc)
		{
			print "<tr><td>".$proc[0]."</td><td align='center'>".$proc[1]."</td></tr>\n";

		}

	}

	function printHostInfo()
	{
		print "<div id='table_header'>";
		print "<img src='images/16x16/sys.png' alt='sys' />Systemdaten";
		print "</div><div id='table_box'>";


		//**************************************************************
		//get hostname and ip

		$hostname=$this->sysinfo->getServerName();
		$IP = $this->sysinfo->getServerAddress();

		//**************************************************************
		//system time
		$sys_time=date(" d.m.Y H:i:s");

		//**************************************************************
		//Kernelversion

		$kernel_version = $this->sysinfo->getKernelVersion();


		//**************************************************************
		//get used ram
		$handle=popen('cat /proc/meminfo | grep "MemTotal"','r');
		$mem_total = fread($handle,1096);
		pclose($handle);

		$handle=popen('cat /proc/meminfo | grep "MemFree"','r');
		$mem_free = fread($handle,1096);
		pclose($handle);

		//$mem_lines=split("\n",$mem);
		$total_mem=preg_replace("/[^0-9]/","",$mem_total);
		$free_mem=preg_replace("/[^0-9]/","",$mem_free);
		$used_mem=$total_mem-$free_mem;


		$mem=$this->sysinfo->getMemoryUsage();

		$free_mem=$mem['free'];
		$total_mem=$mem['total'];


		if($total_mem==0)
			$perc="n/a";
		else
			$perc=intval(($used_mem/$total_mem)*100)."%";




		//free space on "/"
		$root_mem=$df = intval(disk_free_space("/")/1024/1024);


		//**************************************************************
		//uptime...
		$uptime=$this->sysinfo->getUptime();

		//uptime days
		$days=floor($uptime/24);
		$hours=$uptime%24;


		//**************************************************************

		$logged_in_users=$this->sysinfo->getActiveUsers();

		//**************************************************************

		$distro=$this->sysinfo->getOS();

		print "\n<table class='data_table'>";

		$distribution=$this->translator->getString("Distribution");
		$kernel=$this->translator->getString("Kernel");
		$hostname=$this->translator->getString("Hostname");
		$uptime=$this->translator->getString("Uptime");
		$systemtime=$this->translator->getString("System time");
		$memory=$this->translator->getString("Used memory");
		$logged_in=$this->translator->getString("Logged in users");
		$space=$this->translator->getString("Free space on /");

		print "<tr><td>$distribution:</td><td>$distro</td></tr>";
		print "<tr><td>$kernel:</td><td>$kernel_version</td></tr>";
		print "<tr><td>$hostname:</td><td>$hostname  ($IP)</td></tr>";
		print "<tr><td>$uptime:</td><td>$days Tage, $hours Stunden</td></tr>";
		print "<tr><td>$systemtime:</td><td>$sys_time</td></tr>";
		print "<tr><td>$memory:</td><td>$perc</td></tr>";
		print "<tr><td>$logged_in:</td><td>$logged_in_users</td></tr>";
		print "<tr><td>$space:</td><td>$root_mem MB</td></tr>";
		print "</table>";
	}
}
?>
